package com.novabluetooth.services.bluetooth2;

import java.util.Calendar;
import java.util.List;

import com.health.R;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
/**
 * 血糖结果展示
 * @author nova
 *
 */
public class BGResultActivity extends Activity {

	private static final String TAG = "BGResultActivity";
	public static final String BG_ADDR = "address";
	public static final String BG = "bg_value";
	public static final String BG_BUNDLE = "bg_bundle";
	private static final int UPLOAD_DATA_OK = 0;

	private TextView tvTitle, tvXuetang, tvTime;
	private Button uploadBtn, btnBack;
	private Typeface typeface;

	private Intent intent;
	private Bundle bundle;
	private float bg_value;
	private String addr;
	int eatState = 1;

	private String deviceType;

	private SQLiteDatabase db;
	private ContentValues cunValues;

	private SharedPreferences sharedPrefrences;
	private ProgressDialog progressdialog;
	private String path = "";
	private String cardID;

	private String json_message;// json返回的内容

	// 餐前还是餐后
	private RadioButton radio_can1;
	private RadioButton radio_can2;

	private String speechtext = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.ac_bgresult);

		sharedPrefrences = getSharedPreferences("user", MODE_PRIVATE);
		cardID = sharedPrefrences.getString("userCardID", "");
		initView();

		intent = getIntent();
		if (intent != null) {

			bundle = intent.getBundleExtra(BGResultActivity.BG_BUNDLE);
			bg_value = bundle.getFloat(BG, 00.0f);
			addr = bundle.getString(BG_ADDR);
			deviceType = bundle.getString("deviceType");
			tvXuetang.setText("" + bg_value);
			Log.e(TAG, "===== BG_value:" + bg_value + "   BT Addr:  " + addr);
		}

		if (BGResultActivity.isServiceRunning(getApplicationContext(), "nova.main.service.ListenService")) {
			stopService(new Intent(BGResultActivity.this, ListenService.class));
		}
	}

	private static boolean isServiceRunning(Context applicationContext, String serviceName) {
		boolean isRunning = false;
		ActivityManager am = (ActivityManager) applicationContext.getSystemService(ACTIVITY_SERVICE);
		List<RunningServiceInfo> infos = am.getRunningServices(500);
		for (RunningServiceInfo info : infos) {
			if (info.service.getClassName().equals(serviceName)) {
				Log.e("LeeTag", "服务已经运行");
				isRunning = true;
				break;
			}
		}
		return isRunning;
	}

	private void initView() {
		typeface = Typeface.createFromAsset(getAssets(), "fronts/led_front.ttf");
		tvTitle = (TextView) findViewById(R.id.public_top_tvTitle);
		tvTitle.setText("血　糖");

		// 上传按钮
		uploadBtn = (Button) findViewById(R.id.public_top_btnRegister);
		uploadBtn.setVisibility(View.VISIBLE);
		uploadBtn.setText("保存");
		uploadBtn.setOnClickListener(uploadClick);

		// 返回按钮
		btnBack = (Button) findViewById(R.id.public_top_btnBack);
		btnBack.setOnClickListener(cancelClick);

		tvXuetang = (TextView) findViewById(R.id.xuetang);
		tvXuetang.setTypeface(typeface);

		tvTime = (TextView) findViewById(R.id.tv_time);
		tvTime.setText(getTime());
		radio_can1 = (RadioButton) findViewById(R.id.radio_can1);
		radio_can2 = (RadioButton) findViewById(R.id.radio_can2);

	}

	// 保存数据
	private OnClickListener uploadClick = new OnClickListener() {

		@Override
		public void onClick(View v) {

			String value = tvXuetang.getText().toString().trim();// 血糖值
			String time = tvTime.getText().toString().trim();// 测量时间
			String CardID = sharedPrefrences.getString("userCardID", "").trim();// 卡号
		}


	};

	private boolean checkNetError(Context context) {
		try {
			NetworkInfo networkInfo = null;

			ConnectivityManager nw = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			networkInfo = nw.getActiveNetworkInfo();

			return networkInfo.isAvailable();// 网络是否可用
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	/**
	 * 返回
	 */
	private OnClickListener cancelClick = new OnClickListener() {

		public void onClick(View v) {
			BGResultActivity.this.finish();
		}
	};

	private int mYear;
	private int mMonth;
	private int mDay;
	private int mHour;
	private int mMinute;
	private int mSecond;

	protected String getTime() {
		String fmt = null;
		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH) + 1;

		String monStr = mMonth + "";
		if (monStr.length() == 1) {
			monStr = "0" + monStr;
		}

		mDay = c.get(Calendar.DAY_OF_MONTH);
		String dayStr = mDay + "";
		if (dayStr.length() == 1) {
			dayStr = "0" + dayStr;
		}

		mHour = c.get(Calendar.HOUR_OF_DAY);
		String hourStr = mHour + "";
		if (hourStr.length() == 1) {
			hourStr = "0" + hourStr;
		}

		mMinute = c.get(Calendar.MINUTE);
		String minStr = mMinute + "";
		if (minStr.length() == 1) {
			minStr = "0" + minStr;
		}

		mSecond = c.get(Calendar.SECOND);
		String secStr = mSecond + "";
		if (secStr.length() == 1) {
			secStr = "0" + secStr;
		}
		fmt = "" + mYear + "-" + monStr + "-" + dayStr + " " + hourStr + ":"
				+ minStr + ":" + secStr;
		return fmt;
	}



	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			BGResultActivity.this.finish();
		}
		return false;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
}
